from selenium import webdriver
from selenium.webdriver.chrome.service import Service
from selenium.webdriver.common.by import By
from selenium.webdriver.chrome.options import Options
import time

driver_path = Service(executable_path='./chromedriver.exe')
options = Options()
options.add_experimental_option("detach", True)
browser = webdriver.Chrome(service=driver_path, options=options)

browser.get('https://www.baidu.com')

# 打开一个新的标签页
browser.execute_script("window.open()")
print(browser.window_handles)

# 窗口切换
browser.switch_to.window(browser.window_handles[1])
browser.get('https://www.bilibili.com')
time.sleep(2)

browser.switch_to.window(browser.window_handles[0])
browser.get('https://www.12306.cn/')